#pragma once
#include "BufferedWnd.h"
#include "Timer.h"

#define LONG_TIMER_INTERVAL   250

class CSkinnedInt : public CBufferedWnd
{
public:
  CSkinnedInt() : m_val(0), m_min(0), m_max(1),
    m_dragging(false), m_lastX(0), m_longTimer(this), m_longTimerCnt(0),
    m_tapLeft(false), m_tapRight(false)
  { SetImages(CTheme::GetImage(timSliderLeft), CTheme::GetImage(timSliderRight), CTheme::GetImage(timSliderBg), CTheme::GetImage(timSliderBar), CTheme::GetImage(timSliderDBar)); }
  ~CSkinnedInt()
  {}

  void SetImages(const CImage& left, const CImage& right, const CImage& bg, const CImage& bar, const CImage& downBar)
  { m_imgLeft = left; m_imgRight = right; m_imgBg = bg; m_imgBar = bar; m_imgBarDown = downBar; }

  int  GetValue() const
  { return m_val; }
  void SetValue(int val);

  int  GetMin() const
  { return m_min; }
  int  GetMax() const
  { return m_max; }
  void SetRange(int min, int max)
  { m_min = min; m_max = max; if (m_max == m_min) m_max++; }

// Message map and handlers
	BEGIN_MSG_MAP(CSkinnedInt)
    MSG_WM_KEYDOWN(OnKeyDown)
    MSG_WM_KEYUP(OnKeyUp)

    MSG_WM_LBUTTONDOWN(OnLButtonDown)
    MSG_WM_MOUSEMOVE(OnMouseMove)
    MSG_WM_LBUTTONUP(OnLButtonUp)

    CHAIN_MSG_MAP(CBufferedWnd)
	END_MSG_MAP()

  virtual void Draw(CDCHandle dc, const CRect& rClip);

  virtual void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
  virtual void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);

  virtual void OnLButtonDown(UINT nFlags, CPoint point);
  virtual void OnMouseMove(UINT nFlags, CPoint point);
  virtual void OnLButtonUp(UINT nFlags, CPoint point);

  RECT GetBarRect();
  RECT GetLeftRect();
  RECT GetRightRect();

  bool IsPtInRect(int x, int y, const CRect& rc);

  void OnLongTimer(UINT nID, DWORD dwTime);

protected:
  int m_val;
  int m_min;
  int m_max;

  CImage m_imgLeft;
  CImage m_imgRight;
  CImage m_imgBg;
  CImage m_imgBar;
  CImage m_imgBarDown;

  bool m_dragging;
  int  m_lastX;

  CTimerInst<CSkinnedInt, &CSkinnedInt::OnLongTimer> m_longTimer;
  int  m_longTimerCnt;
  bool m_tapLeft;
  bool m_tapRight;
};
